﻿Public Class oPromotionSalesPersonExclude
    Public Function UpdateJsonPromotionSalesPersonEx(ByVal lineID As String, ByVal promotionID As String, _
                                ByVal salerPersonID As String, _
                                ByVal databaseName As String, ByVal isInsert As Boolean) As String
        Dim connect As New Connection()
        Try
            Dim errMsg As String = connect.setSQLDB(databaseName)
            If errMsg.Length = 0 Then
                Dim params = New Object() {promotionID, salerPersonID}
                Dim query As New StringBuilder
                If isInsert = True Then
                    query.Append(" IF NOT EXISTS(SELECT 1 FROM oPromotionSalesPersonExclude WHERE [PromotionID] = @Param1 AND SalesPersonID = @Param2)")
                    query.Append(" INSERT INTO [oPromotionSalesPersonExclude]")
                    query.Append("  ([PromotionID]")
                    query.Append(" ,[SalesPersonID])")
                    query.Append(" VALUES (@Param1,@Param2)")
                    Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), params)
                Else
                    query.Append("UPDATE  [oPromotionSalesPersonExclude] SET ")
                    query.Append("  [SalesPersonID] = @Param1")
                    query.Append(" WHERE [ID] = @Param2")
                    connect.Object_Execute_SAP(query.ToString(), New Object() {salerPersonID, lineID})
                End If
            Else
                Return errMsg
            End If
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
    Public Function DeleteJsonPromotionSalesPersonEx(ByVal dataBaseName As String, ByVal lineID As String) As String
        Dim connect As New Connection()
        Try
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                Dim params = New Object() {lineID}
                Dim query As New StringBuilder
                query.Append(" DELETE FROM [oPromotionSalesPersonExclude]")
                query.Append("  WHERE ID =  @Param1")
                Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), params)
            Else
                Return errMsg
            End If
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
    Public Function GetAllPromotionSalesPersonExByPromotionID(ByVal dataBaseName As String, ByVal promotionID As String) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oPromotionSalesPersonExclude")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            Dim query As New StringBuilder
            If errMsg.Length = 0 Then
                query.Append("SELECT * FROM oPromotionSalesPersonExclude T0 JOIN oSalesPerson T1 ON T0.SalesPersonID = T1.SalesPersonID ")
                query.Append(" WHERE  T0.PromotionID  =  @Param1")
                query.Append("  ORDER BY T0.ID DESC")
                ds = connect.ObjectGetAll_Query_SAP(query.ToString(), New Object() {promotionID})
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
End Class
